<!--
 * @Description:
 * @Author: Morgan
 * @Date: 2021-06-24 17:07:01
 * @LastEditors: Morgan
 * @LastEditTime: 2021-07-02 17:15:29
-->
<template>
  <!-- 资讯列表页面 -->
  <div>
    <!-- 按钮 -->
    <div class="btns">
      <!-- {{ searchForm }} -->
      <!-- --- -->
      <!-- {{ articleData }} -->
      <el-row>
        <el-col :span="16">
          <el-form :inline="true" :model="searchForm" size="small">
            <el-form-item class="">
              <el-select v-model="searchForm.status" clearable placeholder="状态">
                <el-option label="未审核" value="未审核" />
                <el-option label="审核通过" value="审核通过" />
                <el-option label="审核不通过" value="审核不通过" />
                <el-option label="推荐" value="推荐" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input v-model="searchForm.title" placeholder="标题" />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="searchForm.categoryId"
                placeholder="请选择栏目"
                clearable
              >
                <el-option
                  v-for="item in articleCategory"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-button type="primary" size="small" @click="toSearch">查询</el-button>
          </el-form>
        </el-col>
        <el-col :span="8" style="text-align:right">
          <el-button type="primary" size="small" @click="toPublishArticle">发布风采</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- table -->
    <div>
      <!-- {{ articleData }} -->
      <el-table
        v-loading="loading"
        :data="articleData"
        style="width: 100%"
        stripe
        @row-click="toDetails"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column label="封面" width="150px">
          <template slot-scope="scope">
            <img :src="scope.row.cover" alt="" style="width:130px">
          </template>
        </el-table-column>
        <el-table-column label="风采">
          <template slot-scope="scope">
            <div style="font-weight:bold">{{ scope.row.title }}</div>
            <div>{{ scope.row.baseUser && scope.row.baseUser.realname }}</div>
            <div>{{ scope.row.category && scope.row.category.name }}</div>
            {{ moment(scope.row.publishTime).format("YYYY-MM-DD") }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '审核通过'" type="success">{{ scope.row.status }}</el-tag>
            <el-tag v-else-if="scope.row.status === '审核不通过'" type="danger">{{ scope.row.status }}</el-tag>
            <el-tag v-else-if="scope.row.status === '推荐'" type="warning">{{ scope.row.status }}</el-tag>
            <el-tag v-else type="info">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="140">
          <template slot-scope="scope">
            <el-button type="text" :disabled="scope.row.status === '推荐'" class="green_info" @click.stop="toRecommend(scope.row.id,'推荐')">推荐</el-button>
            <el-button type="text" class="blue_info" @click.stop="toCheckArticle(scope.row)">审核</el-button>
            <el-button type="text" class="red_info" @click.stop="toDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination-div">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 审核 -->
    <el-dialog class="customer_modal" title="审核风采信息" :visible.sync="visible" width="60%">
      <div>
        <div style="text-align:center;font-weight:bold"> {{ article.title }}</div>
        <div v-html="article.content" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="checkHandler(article.id,'审核不通过')">审核不通过</el-button>
        <el-button type="primary" @click="checkHandler(article.id,'审核通过')">审核通过</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { pageQuery, changeStatus, deleteById, findById } from '@/api/article'
import { findAll } from '@/api/infoCategory'
import moment from 'moment'

export default {
  data() {
    return {
      articleCategory: [],
      articleData: [],
      // 模糊搜索的表单的数据
      searchForm: {
        status: '',
        title: '',
        categoryId: ''
      },
      loading: false,
      // 分页参数
      params: {
        page: 1,
        pageSize: 10
      },
      // 总条数
      total: 0,
      visible: false,
      article: {}
    }
  },
  computed: {},
  watch: {
    params: {
      handler() {
        this.queryArticleData()
      },
      deep: true
    }
  },
  created() {
    this.queryArticleData()
    // 栏目分类
    this.queryCategoryData()
  },
  mounted() {},
  methods: {
    moment,
    async queryArticleData() {
      this.loading = true
      // 处理数据
      const temp = {
        // 合并数据
        ...this.params,
        ...this.searchForm
      }// 遍历对象
      for (const key in temp) {
        // key是一个变量，代表的属性名
        // console.log(key, temp[key]);
        if (!temp[key]) {
          delete temp[key]
        }
      }
      const res = await pageQuery(temp)
      this.loading = false
      this.articleData = res.data.list
      this.total = res.data.total
    },
    async queryCategoryData() {
      const res = await findAll()
      this.articleCategory = res.data
    },
    // 每页显示的条数发生变化时的处理程序
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.params = {
        pageSize: val,
        page: 1
      }
    },
    // 当前页码发生变化时的事件处理程序
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.params.page = val
    },
    // 模糊查询
    toSearch() {
      this.queryArticleData()
    },
    // 发布风采
    toPublishArticle() {
      this.$router.push({
        path: '/admin/info/article/Publish'
      })
    },
    // 审核风采
    toCheckArticle(row) {
      this.visible = true
      this.article = row
    },
    // 修改风采状态
    async checkHandler(id, status) {
      const data = { id, status }
      const res = await changeStatus(data)
      this.$notify.success({
        title: '成功',
        message: res.success
      })
      this.visible = false
      this.queryArticleData()
    },
    // 推荐风采
    async toRecommend(id, status) {
      this.$confirm('此操作将推荐该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const data = { id, status }
        const res = await changeStatus(data)
        this.$notify.success({
          title: '成功',
          message: res.success
        })
        this.queryArticleData()
      }).catch(() => {
        this.$notify.info({
          title: '取消',
          message: '已取消推荐'
        })
      })
    },
    // 删除风采
    toDelete(id) {
      this.$confirm('此操作将永久删除该风采, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteById({ id })
        this.$notify.success({
          title: '成功',
          message: res.success
        })
        this.queryArticleData()
      }).catch(() => {
        this.$notify.info({
          title: '取消',
          message: '已取消删除'
        })
      })
    },
    // 通过id查找风采
    async findArticleById(id) {
      const res = await findById(id)
    },
    // 风采详情
    toDetails(row) {
      this.$router.push({
        path: '/admin/info/article/Details',
        query: { id: row.id }
      })
    }
  }
}
</script>
<style scoped>
</style>
